import 'package:flutter/material.dart';
import 'popup_menu.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: MyHomePage(title: 'Popup Menu Example'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  PopupMenu menu;
  GlobalKey btnKey = GlobalKey();
  GlobalKey btnKey2 = GlobalKey();

  @override
  void initState() {
    super.initState();

    menu = PopupMenu(items: [
      // MenuItem(title: 'Copy', image: Image.asset('assets/copy.png')),
      // MenuItem(title: 'Home', image: Icon(Icons.home, color: Colors.white,)),
      // MenuItem(title: 'Mail', image: Icon(Icons.mail, color: Colors.white,)),
      // MenuItem(title: 'Power', image: Icon(Icons.power, color: Colors.white,)),
      // MenuItem(title: 'Setting', image: Icon(Icons.settings, color: Colors.white,)),
      MenuItem(
          title: 'PopupMenu',
          image: Icon(
            Icons.menu,
            color: Colors.white,
          ))
    ], onClickMenu: onClickMenu, onDismiss: onDismiss);
  }

  void onClickMenu(MenuItemProvider item) {
    print('Click menu -> ${item.menuTitle}');
  }

  void onDismiss() {
    print('Menu is closed');
  }

  @override
  Widget build(BuildContext context) {
    PopupMenu.context = context;

    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Column(
        children: <Widget>[
          Container(
            child: MaterialButton(
              height: 45.0,
              key: btnKey,
              onPressed: () {
                menu.show(rect: PopupMenu.getWidgetGlobalRect(btnKey));
              },
              child: Text('Show Menu'),
            ),
          ),
          Container(
            child: MaterialButton(
              key: btnKey2,
              height: 45.0,
              onPressed: customBackground,
              child: Text('Show Menu'),
            ),
          )
        ],
      ),
    );
  }

  customBackground() {
    PopupMenu(
            backgroundColor: Colors.teal,
            lineColor: Colors.tealAccent,
            items: [
              MenuItem(
                  title: 'Home',
                  // textStyle: TextStyle(fontSize: 10.0, color: Colors.tealAccent),
                  image: Icon(
                    Icons.home,
                    color: Colors.white,
                  )),
              MenuItem(
                  title: 'Home',
                  // textStyle: TextStyle(fontSize: 10.0, color: Colors.tealAccent),
                  image: Icon(
                    Icons.home,
                    color: Colors.white,
                  )),
              MenuItem(
                  title: 'Mail',
                  image: Icon(
                    Icons.mail,
                    color: Colors.white,
                  )),
            ],
            onClickMenu: onClickMenu,
            onDismiss: onDismiss)
        .show(widgetKey: btnKey2);
  }
}
